

import 'package:flutter/material.dart';
import 'package:flutter_ahj_app/base/basewidget/view/BaseWidget.dart';
import 'package:flutter_ahj_app/base/utils/mTopBar.dart';
import 'package:flutter_ahj_app/base/values/SQColor.dart';
import 'package:flutter_ahj_app/base/values/dimens.dart';
import 'package:flutter_ahj_app/base/widget/LoadingWidget.dart';
import 'package:flutter_ahj_app/base/widget/NoneData.dart';
import 'package:rxdart/rxdart.dart';

import 'KaiHuHangViewModel.dart';
import 'bean/KaiHuHangBean.dart';

class KaiHuHangView extends BaseStatefulWidget {
  Map params;

  KaiHuHangView({Key key, this.params});


  @override
  State<StatefulWidget> initState() {
    return KaiHuHangViewState();
  }
}

class KaiHuHangViewState extends BaseState<KaiHuHangViewModel, KaiHuHangView> {
  List<KaiHuHangDetailBean> listData;
  @override
  void initState() {
    super.initState();



    _textFieldSub = BehaviorSubject<String>();
    _textFieldSub
        .debounceTime(Duration(milliseconds: 500))
        .listen((str){
      _searchContent = str;
      _searchRequest(0);
    });


    Future.delayed(Duration.zero, () {
      viewModel.getData("",widget.params["province"],widget.params["city"]);
      Stream<KaiHuHangBean> stream = viewModel.getDataStream();
      stream.listen((kaiHuHang){
        setState(() {
          listData=kaiHuHang.data;
        });
      });
    });
  }



  ///根据搜索结果 请求接口
  _searchRequest(int type) async {
    viewModel.getData(_searchContent,widget.params["province"],widget.params["city"]);
    Stream<KaiHuHangBean> stream = viewModel.getDataStream();
    stream.listen((kaiHuHang){
      setState(() {
        listData=kaiHuHang.data;
      });
    });

  }


  ///搜索内容
  String _searchContent;
  TextEditingController _controller = TextEditingController(text: '');
  BehaviorSubject<String> _textFieldSub;
  @override
  Widget initWidget() {
    return WillPopScope(
      child: Scaffold(
        appBar: MTopBar(
          child: _searchLayout(),
        ).create(),
        body: _bodyView(),
      ),
    );
  }
  _searchLayout() {
    return Row(
      children: <Widget>[
        GestureDetector(
          onTap: () {
            Navigator.pop(context);
          },
          child: Container(
            width: Dimens.dimens48,
            height: Dimens.dimens48,
            alignment: Alignment.centerLeft,
            child: Icon(
              Icons.arrow_back,
              color: Colors.white,
            ),
          ),
        ),
        Expanded(
          child: Container(
            height: Dimens.dimens36,
            child: TextField(
              controller: _controller,
              maxLines: 1,
              onChanged: (value) {
                _textFieldSub.add(value);
                setState(() {});
              },
              decoration: InputDecoration(
                hintText: "请输入开户行名称",
                hintStyle: TextStyle(fontSize: Dimens.textSize28),
                fillColor: Colors.white,
                //filled = true 时为背景颜色
                filled: true,
                contentPadding:
                EdgeInsets.fromLTRB(Dimens.dimens16, 0, Dimens.dimens16, 0),
                enabledBorder: OutlineInputBorder(
                  borderRadius:
                  BorderRadius.all(Radius.circular(Dimens.dimens4)), //圆角
                  borderSide: BorderSide(color: Colors.white, width: 1), //边框
                ),
                focusedBorder: OutlineInputBorder(
                  borderRadius:
                  BorderRadius.all(Radius.circular(Dimens.dimens4)), //圆角
                  borderSide: BorderSide(color: Colors.white, width: 1), //边框
                ),
                suffixIcon: _controller.text.length > 0
                    ? new IconButton(
                    icon: Icon(
                      Icons.close,
                      color: SQColor.per38Black,
                      size: Dimens.dimens20,
                    ),
                    onPressed: () {
                      setState(() {
                        _controller.clear();
                      });
                      _textFieldSub.add("");
                    })
                    : null,
              ),
            ),
          ),
        ),
        GestureDetector(
          onTap: () {
            _searchRequest(0);
          },
          child: Container(
            padding: EdgeInsets.only(left: Dimens.dimens16),
            height: Dimens.dimens48,
            alignment: Alignment.centerRight,
            child: Text("搜索",
                style: TextStyle(
                    color: SQColor.white, fontSize: Dimens.textSize28)),
          ),
        ),
      ],
    );
  }
  _sure() {
   /* Navigator.pop(
        context, {"province": selProvince, "city": selCity, "area": selArea});*/
  }


  _bodyView() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.start,
      children: <Widget>[
        Expanded(
          flex: 1,
          child: listData == null
              ? LoadingWidget()
              : listData.length == 0
              ? NoneData()
              : ListView.builder(
            shrinkWrap: true,
            physics: new AlwaysScrollableScrollPhysics(),
            itemCount: listData.length,
            itemBuilder: (BuildContext context, int index) {
              return buildModule(context,index);
            },
          ),
        ),

      ],
    );
  }

  ///抽取item项
/*  Widget _getItem(int index) {
 listData[index].name,
            style: TextStyle(
                color:SQColor.black_333333),
  }*/

  buildModule(BuildContext context, int index) {
    return GestureDetector(
      onTap: () => _click(index),
      child:new Column(
        children: <Widget>[
          new  ListTile(
            title: new Text(listData[index].bankName, style: TextStyle(color:SQColor.black_333333)),
          ),
          new Divider()
        ],
      ),
    );

  }


  _click(int index) {
    Navigator.pop(context,listData[index] );
  }




}
